<?php
class rekomendasi extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('tpengaju');
		$this->load->model('tpolice');
		
		$this->load->model('magama');
		$this->load->model('mkebangsaan');
		$this->load->model('msuku');
		$this->load->model('mpekerjaan');
		
		$this->load->model('msurat');
		$this->load->model('mpenandatangan');
	}
	
	function index()
	{
		$sektor = $this->session->userdata('sektor');
		$pengajuan = "2"; // 1=skck 2=rekomendasi
		
		$config['base_url'] = base_url().'rekomendasi/index/';
		$config['total_rows'] = $this->tpengaju->getCountList($sektor,$pengajuan);
		$config['per_page'] = 15;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->tpengaju->getList($sektor,$config['per_page'],$this->uri->segment(3),$pengajuan);
			$this->load->view('rekomendasi/list',$data);
		}else{
			 redirect('login');
		}
	}
    
	function add(){
		$data['mode'] = "tambah";
		$data['action'] = "save";
		
		$data['set_agama'] = $this->magama->getComboList();
		$data['set_kebangsaan'] = $this->mkebangsaan->getComboList();
		$data['set_suku'] = $this->msuku->getComboList();
		$data['set_pekerjaan'] = $this->mpekerjaan->getComboList();
		$this->load->view('rekomendasi/form',$data);
	}
	
	function edit($id_pengaju){
		$data['mode'] = "ubah";
		$data['action'] = "update";
	
		$data['detail_pengaju'] = $this->tpengaju->detail($id_pengaju);
		$data['detail_police'] = $this->tpolice->detail($id_pengaju);
		
		$data['set_agama'] = $this->magama->getComboList();
		$data['set_kebangsaan'] = $this->mkebangsaan->getComboList();
		$data['set_suku'] = $this->msuku->getComboList();
		$data['set_pekerjaan'] = $this->mpekerjaan->getComboList();
		$this->load->view('rekomendasi/form',$data);
	}
	
	function save()
	{
		$datestring = 'Y-m-d';
		
		//pengaju
		$id_pengaju = $this->tpengaju->getNoPengaju();
		$no_identitas_pengaju = $this->input->post('no_identitas_pengaju');
		$no_passport = $this->input->post('no_passport');
		$nama_pengaju = $this->input->post('nama_pengaju');
		$jk_pengaju = $this->input->post('jk_pengaju');
		$tempat_lahir_pengaju = $this->input->post('tempat_lahir_pengaju');
		$tgl_lahir_pengaju = date($datestring, strtotime($this->input->post('tgl_lahir_pengaju')));
		$alamat_pengaju = $this->input->post('alamat_pengaju');
		$id_kebangsaan = $this->input->post('id_kebangsaan');
		$id_agama = $this->input->post('id_agama');
		$id_pekerjaan = $this->input->post('id_pekerjaan');
		$id_suku = $this->input->post('id_suku');
		$id_sektor = $this->session->userdata('sektor');
		
		//data police
		$id_police = $this->input->post('id_police');
		$no_agenda = $this->input->post('no_agenda');
		$no_seri = $this->input->post('no_seri');
		$tgl_terbit_police = date('Y-m-d');
		$tgl_akhir_berlaku = date('Y-m-d');
		$keperluan = $this->input->post('keperluan');
		$sidik_jari_atas = $this->input->post('sidik_jari_atas');
		$sidik_jari_bawah = $this->input->post('sidik_jari_bawah');
		$op_catatan = $this->input->post('op_catatan');
		$pasal = $this->input->post('pasal');
		$undang_undang = $this->input->post('undang_undang');
		$nomor_bulan = $this->input->post('nomor_bulan');
		$nomor_tahun = $this->input->post('nomor_tahun');
		$nomor_sat = $this->input->post('nomor_sat');
		$ttd_perwakilan = $this->input->post('ttd_perwakilan');
		$is_pengajuan = $this->input->post('is_pengajuan');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			//pengaju
			$this->tpengaju->setData($id_pengaju,$no_identitas_pengaju,$no_passport,$nama_pengaju,$jk_pengaju,$tempat_lahir_pengaju,$tgl_lahir_pengaju,$alamat_pengaju,$id_kebangsaan,$id_agama,$id_pekerjaan,$id_sektor,$id_suku,$is_pengajuan);
			$this->tpengaju->create();
			
			//police
			$this->tpolice->setData($id_police,$no_agenda,$no_seri,$no_kwitansi,$tgl_terbit_police,$tgl_akhir_berlaku,$keperluan,$sidik_jari_atas,$sidik_jari_bawah,$op_catatan,$pasal,$undang_undang,$nomor_bulan,$nomor_tahun,$nomor_sat,$id_sektor,$ttd_perwakilan,$id_pengaju,$is_pengajuan);
			$this->tpolice->create();
			
			$this->session->set_flashdata('success', true);
			redirect('rekomendasi');
		}
		$this->session->set_flashdata('error', true);
		redirect('rekomendasi');
	}
	
	function update()
	{
		$datestring = 'Y-m-d';
		
		//pengaju
		$id_pengaju = $this->input->post('id_pengaju');
		$no_identitas_pengaju = $this->input->post('no_identitas_pengaju');
		$no_passport = $this->input->post('no_passport');
		$nama_pengaju = $this->input->post('nama_pengaju');
		$jk_pengaju = $this->input->post('jk_pengaju');
		$tempat_lahir_pengaju = $this->input->post('tempat_lahir_pengaju');
		$tgl_lahir_pengaju = date($datestring, strtotime($this->input->post('tgl_lahir_pengaju')));
		$alamat_pengaju = $this->input->post('alamat_pengaju');
		$id_kebangsaan = $this->input->post('id_kebangsaan');
		$id_agama = $this->input->post('id_agama');
		$id_pekerjaan = $this->input->post('id_pekerjaan');
		$id_suku = $this->input->post('id_suku');
		$id_sektor = $this->session->userdata('sektor');
		
		//data police
		$id_police = $this->input->post('id_police');
		$no_agenda = $this->input->post('no_agenda');
		$no_seri = $this->input->post('no_seri');
		$tgl_terbit_police = date('Y-m-d');
		$tgl_akhir_berlaku = date('Y-m-d');
		$keperluan = $this->input->post('keperluan');
		$sidik_jari_atas = $this->input->post('sidik_jari_atas');
		$sidik_jari_bawah = $this->input->post('sidik_jari_bawah');
		$op_catatan = $this->input->post('op_catatan');
		$pasal = $this->input->post('pasal');
		$undang_undang = $this->input->post('undang_undang');
		$nomor_bulan = $this->input->post('nomor_bulan');
		$nomor_tahun = $this->input->post('nomor_tahun');
		$nomor_sat = $this->input->post('nomor_sat');
		$ttd_perwakilan = $this->input->post('ttd_perwakilan');
		$is_pengajuan = $this->input->post('is_pengajuan');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			//pengaju
			$this->tpengaju->setData($id_pengaju,$no_identitas_pengaju,$no_passport,$nama_pengaju,$jk_pengaju,$tempat_lahir_pengaju,$tgl_lahir_pengaju,$alamat_pengaju,$id_kebangsaan,$id_agama,$id_pekerjaan,$id_sektor,$id_suku,$is_pengajuan);
			$this->tpengaju->update($id_pengaju);
			
			//police
			$this->tpolice->setData($id_police,$no_agenda,$no_seri,$no_kwitansi,$tgl_terbit_police,$tgl_akhir_berlaku,$keperluan,$sidik_jari_atas,$sidik_jari_bawah,$op_catatan,$pasal,$undang_undang,$nomor_bulan,$nomor_tahun,$nomor_sat,$id_sektor,$ttd_perwakilan,$id_pengaju,$is_pengajuan);
			$this->tpolice->update($id_pengaju);
			
			$this->session->set_flashdata('success', true);
			redirect('rekomendasi');
		}
		$this->session->set_flashdata('error', true);
		redirect('rekomendasi');
	}
	
	function delete($id_pengaju)
	{
		if ($this->tpengaju->remove($id_pengaju) && $this->tpolice->remove($id_pengaju)){
			$this->session->set_flashdata('delete', true);
			redirect('rekomendasi','refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('rekomendasi');
	}
	
	function searchData(){
		$name = $this->input->post('name');
		$bulan = $this->input->post('bulan');
		$pengajuan = "2"; // 1=skck 2=rekomendasi
		if($bulan == 0){
			$bulan = date('m');
		}
		$tahun = $this->input->post('tahun');
		if($tahun == 0){
			$tahun = date('Y');
		}
		$sektor = $this->session->userdata('sektor');
		$data['results'] = $this->tpengaju->getListSearch($sektor,$name,$bulan,$tahun,$pengajuan);
		$this->load->view('rekomendasi/list',$data);
	}
	
	function printData($id_pengaju){
		$sektor = $this->session->userdata('sektor');
		$posisi_kiri="1";
		
		$data['surat'] = $this->msurat->getDetSektor($sektor);
		$data['penandatangan_kiri'] = $this->mpenandatangan->getdatasurat($sektor,$posisi_kiri,"1");
		$data['penandatangan_pihak_lain'] = $this->mpenandatangan->getdatasurat($sektor,$posisi_kiri,"2");
		$data['print_results'] = $this->tpengaju->printData($id_pengaju);
		$this->load->view('rekomendasi/print',$data);	
	}
}
?>